package com.hxf.web.action.food;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.PublicUtil;
import com.hxf.dto.MealMenuTypeDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

/**
 * 餐厅菜肴分类
 * @author Administrator
 *
 */
@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class MealmenutypeAction extends WebActionBase {

	private static final long serialVersionUID = 1L;
	private List<MealMenuTypeDto> mealMenuTypeDtoList;
	private MealMenuTypeDto mmtDto;
	private Integer id;
	private String menuTypeName;
	private Integer status;
	private Integer restaurantId;
	
	/**
	 * 菜肴列表
	 * @return
	 */
	public String mealMenuTypeList() {
		try {
			MealMenuTypeDto mmt = new MealMenuTypeDto();
			RestaurantDto restDto = new RestaurantDto();
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session
			
			List restDtoList = null;
			if(null != adminUserDto){
				restDtoList = super.serviceFacade.getRestaurantService().findByRestaurant(getAdminUser().getId());
			}else if(null != restaurentUserDto){
				restDtoList = super.serviceFacade.getRestaurantService().findByRestaurant(getRestaurentUserDto().getId());
			}
			
			if (restDtoList != null) {
				restDto = (RestaurantDto)restDtoList.get(0);
				if(PublicUtil.isNotNull(getSession().getAttribute("restaurantId"))){
					//mmt.setRestaurantId(restDto.getId());
					mmt.setRestaurantId(Integer.valueOf(getSession().getAttribute("restaurantId").toString()));
				}
				
				mmt.setMenuTypeName(menuTypeName);
				mealMenuTypeDtoList = super.serviceFacade.getMealMenuService().findByMealMenuType(mmt);
			}
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "meal-menu-type-list";
	}
	
	/**
	 * 跳转添加或修改菜肴分类页面
	 * @return
	 */
	public String mealMenuType() {
		try {
			if (id != null && id != 0) {
				mmtDto = (MealMenuTypeDto)super.serviceFacade.getMealMenuService().get(MealMenuTypeDto.class, id);
			}
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} 
		return "meal-menu-type";
	}
	
	/**
	 * 菜肴类型新增或者修改
	 * @return
	 */
	public String mealMenuTypeAddOrUpdate() {
		try {
			if (id != null && id != 0) {
				MealMenuTypeDto typeDto = (MealMenuTypeDto)super.serviceFacade.getMealMenuService().get(MealMenuTypeDto.class, id);
				typeDto.setMenuTypeName(menuTypeName);
				typeDto.setStatus(status);
				if(PublicUtil.isNotNull(getSession().getAttribute("restaurantId"))){
					typeDto.setRestaurantId(Integer.valueOf(getSession().getAttribute("restaurantId").toString()));
				}
				super.serviceFacade.getMealMenuService().update(typeDto);
			} else {
				MealMenuTypeDto menuTypeDto = new MealMenuTypeDto();
				//RestaurantDto restDto = new RestaurantDto();
				
				Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
				Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
				//List restDtoList = null;
				
				if(null != adminUserDto){
					//restDtoList = super.serviceFacade.getRestaurantService().findByRestaurantId(getAdminUser().getId());
					menuTypeDto.setRestaurantId(getAdminUser().getId());
				}else if(null != restaurentUserDto){
					//插入 餐厅Id
					if(PublicUtil.isNotNull(getSession().getAttribute("restaurantId"))){
						menuTypeDto.setRestaurantId(Integer.valueOf(getSession().getAttribute("restaurantId").toString()));
					}
					
					//menuTypeDto.setRestaurantId(getRestaurentUserDto().getId());
					//restDtoList = super.serviceFacade.getRestaurantService().findByRestaurantId(getRestaurentUserDto().getId());
				}
					
				menuTypeDto.setMenuTypeName(menuTypeName);
				menuTypeDto.setStatus(status);
				super.serviceFacade.getMealMenuService().save(menuTypeDto);
			}
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} 
		requestURL = "mealmenutype!mealMenuTypeList.html";
		return SUCCESS;
	}

	/**
	 * 删除菜肴分类
	 * @return
	 */
	public String mealMenuTypeDelete() {
		try {
			MealMenuTypeDto mmtd = new MealMenuTypeDto();
			mmtd.setId(id);
			super.serviceFacade.getMealMenuService().delete(mmtd);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	public List<MealMenuTypeDto> getMealMenuTypeDtoList() {
		return mealMenuTypeDtoList;
	}

	public void setMealMenuTypeDtoList(List<MealMenuTypeDto> mealMenuTypeDtoList) {
		this.mealMenuTypeDtoList = mealMenuTypeDtoList;
	}

	public MealMenuTypeDto getMmtDto() {
		return mmtDto;
	}

	public void setMmtDto(MealMenuTypeDto mmtDto) {
		this.mmtDto = mmtDto;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getMenuTypeName() {
		return menuTypeName;
	}

	public void setMenuTypeName(String menuTypeName) {
		this.menuTypeName = menuTypeName;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getRestaurantId() {
		return restaurantId;
	}

	public void setRestaurantId(Integer restaurantId) {
		this.restaurantId = restaurantId;
	}
}
